package com.topisv.tms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.BackupTask;
import com.topisv.tms.entity.BackupTaskSku;
import com.topisv.tms.entity.ShippedBatch;
import com.topisv.tms.entity.ShippedBox;
import com.topisv.tms.entity.ShippedBoxSkus;
import com.topisv.tms.repository.BackupTaskDao;
import com.topisv.tms.repository.BackupTaskSkuDao;
import com.topisv.tms.repository.ShippedBatchDao;
import com.topisv.tms.repository.ShippedBoxDao;
import com.topisv.tms.repository.ShippedBoxSkusDao;
import com.topisv.tms.utils.BackupTaskRule;

@Component
@Transactional(readOnly=true)
public class BackupTaskSkuService {

	@Autowired
	private BackupTaskSkuDao backupTaskSkuDao;
	@Autowired
	private ShippedBoxSkusDao shippedBoxSkusDao;
	@Autowired
	private BackupTaskDao backupTaskDao;
	@Autowired
	private ShippedBatchDao shippedBatchDao;
	@Autowired
	private ShippedBoxDao shippedBoxDao;
	
	public List<BackupTaskSku> findByBackupTaskCodeAndPickStatus(String backupTaskCode,String pickStatus){
		return backupTaskSkuDao.findByBackupTaskCodeAndPickStatus(backupTaskCode, pickStatus);
	}
	
	public List<BackupTaskSku> findByBackupTaskCodeAndStatus(String backupTaskCode,String status){
		return backupTaskSkuDao.findByBackupTaskCodeAndStatus(backupTaskCode, status);
	}
	
	public BackupTaskSku findByBackupTaskCodeAndSkuId(String backupTaskCode,String skuId){
		return backupTaskSkuDao.findByBackupTaskCodeAndSkuId(backupTaskCode, skuId);
	}
	
	public List<BackupTaskSku> findByBackupTaskCode(String backupTaskCode){
		return backupTaskSkuDao.findByBackupTaskCode(backupTaskCode);
	}
	@Transactional(readOnly=false)
	public BackupTaskSku save(BackupTaskSku arg0){
		return backupTaskSkuDao.save(arg0);
	}
	@Transactional(readOnly=false)
	public List<BackupTaskSku> save(List<BackupTaskSku> arg0){
		return (List<BackupTaskSku>) backupTaskSkuDao.save(arg0);
	}
	
	
	/**
	 * 根据拣货任务号一键装箱
	 * @param backupTaskCode
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	public ShippedBox skuEnchase(String backupTaskCode)throws Exception{
		BackupTask backupTask=null;
		ShippedBoxSkus shippedBoxSkus=null;
		List<BackupTaskSku> backupTaskSkus=null;
		ShippedBox shippedBox=null;
		ShippedBatch shippedBatch = null;
		try {
			backupTask=backupTaskDao.findByBackupTaskCode(backupTaskCode);
			if(backupTask==null){
				return null;
			}
			if("finish".equals(backupTask.getStatus())){
				return null;
			}
			String boxCode = BackupTaskRule.getBoxCode(backupTaskCode);// 箱子条码
			shippedBatch = shippedBatchDao.findByBackupTaskCode(backupTaskCode);
			List<ShippedBox> shippedBoxs=shippedBoxDao.findByBatchCode(shippedBatch.getBatchCode());

			if(!shippedBoxs.isEmpty()){
				return null;
			}
			shippedBox = new ShippedBox();
			shippedBox.setCreateDate(new Date());
			shippedBox.setUpdateDate(new Date());
			shippedBox.setBoxCode(boxCode);
			shippedBox.setSkuQty(shippedBatch.getSkuQty());
			shippedBox.setStatus("pending");
			shippedBox.setType(shippedBatch.getType());
			shippedBox.setBatchCode(shippedBatch.getBatchCode());
			shippedBox.setSkuType(shippedBatch.getSkuType());
			shippedBoxDao.save(shippedBox);
			shippedBatch.setBoxQty(1);// 箱子数量增加
			shippedBatch.setBoxStatus("finish");
			shippedBatchDao.save(shippedBatch);//保存出库批次
			backupTaskSkus=backupTaskSkuDao.findByBackupTaskCode(backupTaskCode);//根据拣货任务号查找sku
			//保存箱子
			for(BackupTaskSku b:backupTaskSkus){
				shippedBoxSkus=new ShippedBoxSkus();
				shippedBoxSkus.setSkuId(b.getSkuId());
				shippedBoxSkus.setRealQty(b.getRealQty());
				shippedBoxSkus.setBatchCode(backupTask.getBatchCode());
				shippedBoxSkus.setBackupTaskCode(backupTaskCode);
				shippedBoxSkus.setBoxCode(boxCode);
				shippedBoxSkusDao.save(shippedBoxSkus);
			}
		
			backupTask.setStatus("finish");
			backupTaskDao.save(backupTask);
		} catch (Exception e) {
			throw e;
		}
		return shippedBox;
	}
	
	/**
	 * 商品扫描装箱
	 * @param backupTaskCode
	 * @param skuId
	 * @return
	 */
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	public BackupTaskSku skuEnchase(String backupTaskCode,String skuId)throws Exception{
		BackupTask backupTask=null;
		BackupTaskSku backupTaskSku=null;
		ShippedBoxSkus shippedBoxSkus=null;
		boolean fa=false;
		List<ShippedBoxSkus> shippedBoxSkusLists = null;
		try {
			backupTask=backupTaskDao.findByBackupTaskCode(backupTaskCode);
			if(backupTask==null){
				return null;
			}
			if("finish".equals(backupTask.getStatus())){
				return null;
			}
			backupTaskSku=backupTaskSkuDao.findByBackupTaskCodeAndSkuId(backupTaskCode, skuId);
			if(backupTaskSku==null){
				return null;
			}
			if("finish".equals(backupTaskSku.getStatus())){
				return null;
			}
			skuId=backupTaskSku.getSkuId();
			int checkQty=backupTaskSku.getCheckQty()+1;
			if(checkQty>backupTaskSku.getRealQty()){
				return null;
			}else if(backupTaskSku.getRealQty()==checkQty){
				backupTaskSku.setStatus("finish");
			}else{
				backupTaskSku.setStatus("pending");
			}
			backupTaskSku.setCheckQty(checkQty);
			backupTaskSkuDao.save(backupTaskSku);
			//根据拣货任务号查找商品是否装箱
			shippedBoxSkusLists = shippedBoxSkusDao.findByBoxCodeAndBackupTaskCode(null,backupTaskCode);
			if(!shippedBoxSkusLists.isEmpty()){
				for(ShippedBoxSkus s:shippedBoxSkusLists){
					if(skuId.equals(s.getSkuId())){
						s.setRealQty(s.getRealQty()+1);
						shippedBoxSkusDao.save(s);
						fa=true;
						break;
					}
				}
			}
			//没有装箱就新增
			if(!fa){
				shippedBoxSkus=new ShippedBoxSkus();
				shippedBoxSkus.setSkuId(skuId);
				shippedBoxSkus.setRealQty(1);
				shippedBoxSkus.setBatchCode(backupTask.getBatchCode());
				shippedBoxSkus.setBackupTaskCode(backupTaskCode);
				shippedBoxSkusDao.save(shippedBoxSkus);
			}
		} catch (Exception e) {
			throw e;
		}
		return backupTaskSku;
	}
}
